import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)



import manList from '../pages/manList'
import payCar from '../pages/payCar'
import shop from '../pages/shop'
import shopList from '../pages/shopList'
import shopDetail from '../pages/detail'


const router = new VueRouter({
    routes:[

        // {
        //     path:'login',
        //     name:'login',
        //     redirect:'/manList',
        //     afterEnter(to,from,next){
        //         console.log('beforeEnter--',to,from)
        //         if(to.query.username === 'root'){
        //             next()
        //         }else{
        //             next({
        //               name:'shopIndex'
        //             })
        //         }
        //     }
        // },


        {
            path:'/shop',
            name:'shopIndex',
            component:shop,
            props(route){
                return {
                    name:route.query.username,
                }
            },
            meta:{
                title:'商品列表'
            },
            children:[
                {
                    path:'payCar/:name',
                    name:'payCar',
                    meta:{
                        title:'购物车'
                    },
                    props(route){
                        return{
                            username:route.params.name,
                        }
                    },
                    component:payCar
                },
                {
                    path:'detail',
                    name:'shopDetail',
                    meta:{
                        title:'商品详情'
                    },
                    component:shopDetail,
                    props:true
                },
                {
                    path:'shopList',
                    name:'shopList',
                    component:shopList,
                },
            ]
        },
        {
            path:'/manager',
            name:'manList',
            props(route){
                return {
                    username:route.query.username,
                }
            },
            beforeEnter(to,from,next){
                console.log('beforeEnter',to,from)
                if(to.query.username === 'root' && to.query.password === 'root'){
                    next()
                }else if(to.query.username ==='manager' && to.query.password === 'manager'){
                    next({
                      name:'shopIndex'
                    })
                }else{
                    //登录失败三次  出验证码
                    
                }
            },
    
            meta:{
                title:'后台管理页面'
            },
            component:manList
        },
    ]
})

router.beforeEach((to,from,next)=>{
    //to 到哪去，from 打哪来，next()
    console.log('beforeEach',to,from)
    if(to.meta.isshouwei === true ){
        if(to.query.name === 'root' || to.query.name === 'manage'){
            next()
        }
    }else{
        next()
    }

    // if(to.meta.isAuth){ //判断当前路由是否需要进行权限控制
    //     if(localStorage.getItem('school') === 'atguigu'){ //权限控制的具体规则
    //         next() //放行
    //     }else{
    //         alert('暂无权限查看')
    //         // next({name:'guanyu'})
    //     }
    // }else{
    //     next() //放行
    // }
})

//全局后置守卫：初始化时执行、每次路由切换后执行
router.afterEach((to,from)=>{
    console.log('afterEach',to,from)
    if(to.meta.title){
        document.title = to.meta.title //修改网页的title
    }else{
        document.title = 'Z'
    }
})

export default router
